import {
  DownOutlined,
  EllipsisOutlined,
  QuestionCircleOutlined,
} from '@ant-design/icons';
import type { ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { Button, Tag, Tooltip, Rate, message } from 'antd';
import React from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import { useRequest } from 'umi';
import moment from 'moment';
import { ModalForm, ProFormRate } from '@ant-design/pro-components';

export default () => {
  //学生端-参考文献索引库
  const getReference = useRequest((data = {}) => {
    let url = '';
    let count = 0;
    Object.keys(data).map((value: any, index: any) => {
      // console.log(value);
      let key_temp = value;
      let value_temp = data[value];
      if (data[value]) {
        if (count == 0) {
          url = url + '?' + key_temp + '=' + value_temp;
        } else {
          url = url + '&' + key_temp + '=' + value_temp;
        }
        count++;
      }
    });
    return {
      url: `http://10.1.40.84:9998/stuTrain/getReference` + url,
      method: 'GET',
      headers: {
        Authorization: localStorage.getItem('token') || '',
      },
    };
  });
  //获取 打分
  const scoreMaterial = useRequest(
    (data = {}) => {
      let url = '';
      let count = 0;
      Object.keys(data).map((value: any, index: any) => {
        // console.log(value);
        let key_temp = value;
        let value_temp = data[value];
        if (data[value]) {
          if (count == 0) {
            url = url + '?' + key_temp + '=' + value_temp;
          } else {
            url = url + '&' + key_temp + '=' + value_temp;
          }
          count++;
        }
      });
      return {
        url: `http://10.1.40.84:9998/stuTrain/scoreMaterial` + url,
        method: 'GET',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
      };
    },
    {
      manual: true,
      formatResult: (res) => res,
    },
  );

  const columns = [
    {
      title: '材料名称',
      dataIndex: 'materialName',
      copyable: true,
      ellipsis: true,
    },
    {
      title: '上传学生',
      dataIndex: 'stuName',
      key: 'stuName',
    },
    {
      title: '研究方向',
      dataIndex: 'resDirection',
      key: 'resDirection',
    },
    {
      title: '上传时间',
      key: 'createTime',
      dataIndex: 'createTime',
      hideInSearch: true,
      render: (text: any) => moment(text).format('YYYY-MM-DD'),
    },
    {
      title: '平均评分',
      dataIndex: 'avgScore',
      key: 'avgScore',
      hideInSearch: true,
      sorter: (a: any, b: any) => a.avgScore > b.avgScore,
      render: (text: number) => <Rate defaultValue={text} disabled />,
    },
    {
      title: '操作',
      align: 'center',
      key: 'option',
      valueType: 'option',
      render: (text: any, record: any) => [
        <Button
          disabled={record?.materialContent ? false : true}
          type="link"
          key="1"
          onClick={() => {
            window.open(record.materialContent);
          }}
        >
          查看材料
        </Button>,
        <ModalForm
          key="2"
          title="评分"
          trigger={<a>评分</a>}
          autoFocusFirstInput
          modalProps={{
            onCancel: () => console.log('run'),
          }}
          submitTimeout={2000}
          onFinish={async (values) => {
            scoreMaterial
              .run({
                studentId: record.studentId,
                materialId: record.id,
                score: values.score,
              })
              .then((e) => {
                getReference.run();
                // setIsModalVisible(false);
                message.success(e.msg);
              });
            return true;
          }}
          width={250}
        >
          <ProFormRate name="score" />
        </ModalForm>,
      ],
    },
  ];

  return (
    <ProTable
      columns={columns}
      dataSource={getReference?.data || []}
      rowKey={() => Math.random()}
      pagination={{
        showQuickJumper: true,
      }}
      dateFormatter="string"
      headerTitle="参考文献检索库"
      options={false}
      onSubmit={(e) => getReference.run(e)}
      onReset={() => getReference.run()}
    />
  );
};
